import { JsonController, Param, Body, Get, Post, QueryParam } from 'routing-controllers';
import { Like } from 'typeorm';
import { PositionEntity } from '../entity/position.entity';
import { message } from '../utils/result';
import { list, save } from '../service/position.service';
import { Code } from '../utils/code';

@JsonController('/position')
export class PositionController {
  @Get('/list/:eid')
  async list(@Param('eid') eid: string) {
    const result = await list({ eid });
    if (result[1] == 0) {
      return message(Code.ERROR_RESUME_SELECT, '暂无数据');
    }
    return message(Code.SUCCESS, '获取成功', result[0]);
  }

  @Get('/list')
  async listByCondition(@QueryParam('name') name: string, @QueryParam('treat') treat: string) {
    let condition: any = {};
    if (name) {
      condition.name = Like(`%${name}%`);
    }
    if (treat) {
      condition.treatment = Like(`%${treat}%`);
    }
    const result = await list(condition);
    if (result[1] == 0) {
      return message(Code.ERROR_RESUME_SELECT, '暂无数据');
    }
    return message(Code.SUCCESS, '获取成功', result[0]);
  }

  @Post('/save/:eid')
  async save(@Param('eid') eid: string, @Body() entity: PositionEntity) {
    const index = await save(eid, entity);
    if (index == 0) {
      return message(Code.ERROR_POSI_ADD, '保存失败');
    }
    return message(Code.SUCCESS, '保存成功');
  }
}
